<?php
/*
 * @Author: cwj
 * @Date: 2019-12-13 16:13:22
 * @LastEditTime : 2019-12-24 10:08:40
 * @LastEditors  : Please set LastEditors
 * @Description: 修改 异步方式
 * @FilePath: /ahsmp_3/application/asmpasync/socket/SocketPackage.php
 */
namespace app\asmpasync\socket;

class SocketPackage
{

    public $host_id = 0; //主机id
    public $module = ''; //模块字符串
    public $sync = false; //是否需要返回
    /**
     * @name: 设置头
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-05-28 13:57:01
     */
    public function addHeader ($msg)
    {
        $length = strlen($msg);

        $return = pack('icc', $length, 0, 0) . $msg ;
        
        return $return;
    }

    /**
     * @name: 包裹 代理层
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-07 10:36:38
     */
    public function packPorxy($msg)
    {
        $Any = new \Google\Protobuf\Any();
        $Any->pack($msg);
        
        $Event = new \Proxy\Event();
        $Event->setName(strtolower($this->module));
        $Event->setHostId($this->host_id);
        $Event->setMessage($Any);
        
        return $Event;
    }

    /**
     * @name: 包裹事件层
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-07 10:46:28
     */
    public function packAsmpproto ($msg)
    {
        $id = time();

        $Any = new \Google\Protobuf\Any();
        $Any->pack($msg);
        $Event = new \Asmpproto\Event();
        $Event->setId($id);
        $Event->setSync($this->sync);
        $Event->setMessage($Any);
        $return = $Event->serializeToString();

        return $return;
    }
    /**
     * @name: 数据包
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-05-28 14:45:00
     */
    public function dataPackage ($msg)
    {
        $msg = $this->packPorxy($msg);
        $msg = $this->packAsmpproto($msg);

        $return = $this->addHeader($msg);

        return $return;
    }

}